package com.example.domain.dto.pricing;

import com.example.domain.entity.PricingRule;
import com.example.domain.entity.enums.PricingRuleType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 优惠规则摘要DTO
 * 
 * @author Means
 * @since 2025-10-27
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PricingRuleDTO {
    
    /**
     * 规则ID
     */
    private String id;
    
    /**
     * 规则名称
     */
    private String name;
    
    /**
     * 规则类型
     */
    private PricingRuleType type;
    
    /**
     * 规则类型描述
     */
    private String typeDesc;
    
    /**
     * 规则描述
     */
    private String description;
    
    /**
     * 优先级（数值越小优先级越高）
     */
    private Integer priority;
    
    /**
     * 是否启用
     */
    private Boolean enabled;
    
    /**
     * 生效开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime effectiveFrom;
    
    /**
     * 生效结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime effectiveTo;
    
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    /**
     * 从实体类创建DTO
     * 
     * @param rule 优惠规则实体
     * @return PricingRuleDTO
     */
    public static PricingRuleDTO from(PricingRule rule) {
        if (rule == null) {
            return null;
        }
        
        return PricingRuleDTO.builder()
                .id(rule.getId())
                .name(rule.getName())
                .type(rule.getType())
                .typeDesc(getTypeDescription(rule.getType()))
                .priority(rule.getPriority())
                .enabled(rule.getEnabled())
                .effectiveFrom(rule.getValidFrom())
                .effectiveTo(rule.getValidTo())
                .createdAt(rule.getCreatedAt())
                .updatedAt(rule.getUpdatedAt())
                .build();
    }
    
    /**
     * 获取规则类型描述
     * 
     * @param type 规则类型
     * @return 类型描述
     */
    private static String getTypeDescription(PricingRuleType type) {
        if (type == null) {
            return null;
        }
        switch (type) {
            case ITEM_DISCOUNT:
                return "单品折扣";
            case THRESHOLD_DISCOUNT:
                return "满减折扣";
            default:
                return type.name();
        }
    }
}

